package cn.donting.plugin.spring.boot.starter.utile;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class ClassUtil {



    public static void main(String[] args) throws IOException {
        String s="/Volumes/开发/Web desktop/server/appTest/target/appTest-0.0.1-SNAPSHOT-jar-with-dependencies.jar" ;
        List<String> classesPackage = getClassesPackage(s);
        for (String class1 : classesPackage) {
            System.out.println(class1);
        }
    }

    public static List<String> getClassesPackage(String path) throws IOException {

        JarFile jarFile = new JarFile(path);
        Enumeration<JarEntry> entrys = jarFile.entries();
        List<String> classPackages = new ArrayList<>();
        while (entrys.hasMoreElements()) {
            JarEntry jarEntry = entrys.nextElement();
            if (jarEntry.getName().length() > 7 && jarEntry.getName().substring(jarEntry.getName().length() - 6).equals(".class")) {
                String classPackage = jarEntry.getName().replaceAll("/", ".");
                classPackage = classPackage.substring(0, classPackage.length() - 6);
                classPackages.add(classPackage);
            }
        }
        return classPackages;
    }

}


